﻿using NHibernate;
using NHibernate.Cfg;
using NHibernateStudy.OneToMany;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHibernateStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            Configuration cfg = new Configuration();

            MemoryStream stream = new MemoryStream();
            NHibernate.Mapping.Attributes.HbmSerializer.Default.Validate = true;
            NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
            stream, System.Reflection.Assembly.GetExecutingAssembly());
            stream.Position = 0; // Rewind
            cfg.Configure();
            cfg.AddInputStream(stream); // Use the stream here
            stream.Close();

            ISessionFactory sessionFactory = cfg.BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();

            #region 添加一条数据
            session.Delete("from People;");
            session.BeginTransaction().Commit();

            People p = new People();
            p.Name = "addp" + DateTime.Now.ToString("MMddHHmmss");
            p.Sex = false;
            p.Age = 25;
            p.FirstName = "first" + DateTime.Now.ToString("MMddH");
            p.LastName = "last" + DateTime.Now.ToString("Hmmss");
            session.Save(p);
            session.BeginTransaction().Commit();
            session.Evict(p);
            #endregion

            #region 显示数据库数据
            //session.Clear();
            IList<People> ps = session.CreateCriteria<People>().List<People>();
            foreach (var item in ps)
            {
                Console.WriteLine("ID:" + item.Id + "   Name:" + item.Name + "  Sex:" + item.Sex + "  Age:" + item.Age);
                Console.WriteLine("FirstName:" + item.FirstName + "    FullName:" + item.FullName + " TempFullName:" + item.TempFullName);
            }
            #endregion
            Console.WriteLine("------------------割---------------------");

            #region 添加一对多关联数据
            session.Delete("from Parent;");
            session.BeginTransaction().Commit();

            Parent parent = new Parent();
            parent.Name = "parent" + DateTime.Now.ToString("MMddHHmmss");
            parent.Children = new List<Children>();
            Children child = new Children();
            child.Name = "child" + DateTime.Now.ToString("MMddHHmmss");
            child.TheParent = parent;
            parent.Children.Clear();
            parent.Children.Add(child);
            session.Save(parent);
            session.BeginTransaction().Commit();
            #endregion

            #region 查看多对一数据
            IList<Children> children = session.CreateCriteria<Children>().List<Children>();
            foreach (var item in children)
            {
                Parent itemparent = item.TheParent ?? new Parent();
                Console.WriteLine(string.Format("ChildId:{0} Name:{1}    \r\nParentId:{2} ParentName:{3}", item.Id, item.Name, itemparent.Id, itemparent.Name));
            }
            #endregion

            Console.WriteLine("------------------割---------------------");

            #region NHibernate.Mapping.Attributes映射添加一条数据
            session.Delete("from Animal;");
            session.BeginTransaction().Commit();
            Animal a = new Animal();
            a.Name = "animal" + DateTime.Now.ToString("MMddHHmmss");
            session.Save(a);
            session.BeginTransaction().Commit();

            IList<Animal> an = session.CreateCriteria<Animal>().List<Animal>();
            foreach (var item in an)
            {
                Console.WriteLine("Animal——ID: " + item.Id + "  Name:" + item.Name);
            }
            #endregion

            sessionFactory.Close();
            Console.WriteLine("nhibernate.db连接关闭！");

            Console.ReadKey();
        }
    }
}
